package fm.dongman.modellib.remote;

import android.support.annotation.NonNull;

import org.json.JSONObject;

import java.util.List;

import fm.dongman.animefm.contractlib.BaseDataSource;
import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.contractlib.contract.RankInfoContract;
import fm.dongman.modellib.RemoteHelper;
import fm.dongman.modellib.StaticArea.StaticUrl;

import static fm.dongman.animefm.contractlib.contract.RankInfoContract.IRankInfoPresenter.Companion;

/**
 * @author alex on 2017/10/28.
 */

public class RankInfoRemoteDataSource implements RankInfoContract.IRankInfoDataSource {
    private static final int mPageSize = 10;     //每页数据数量
    private static RankInfoRemoteDataSource INSTANCE; // 实例

    private int mCurrentPage = 0;    //当前页数
    private String mSearchType = Companion.getTYPE_WEEK_HOT();  //榜单类型
    private boolean isRemote = true;

    private RankInfoRemoteDataSource() {
    }

    public static RankInfoRemoteDataSource getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new RankInfoRemoteDataSource();
        }
        return INSTANCE;
    }

    @Override
    public void setRankInfo(int page, @NonNull String type) {
        mCurrentPage = page;
        mSearchType = type;
    }

    @Override
    public void getData(@NonNull BaseDataSource.LoadSourceCallback<? super List<? extends IModel.IComicModel>> callback) {
        if (isRemote) {
            JSONObject object = RemoteHelper.Companion.getInstance().parseGetUrl(StaticUrl.RankInfo + "tag=" + mSearchType + "&page=" + mCurrentPage + "&size=" + mPageSize);
            if (object == null) {
                callback.onDataLoadFiled();
                return;
            }
            callback.onDataLoaded(RemoteOperationHelper.getOperationHelper().downRankDetailsList(object));
        }else callback.onDataLoadFiled();
    }

    @Override
    public void saveData(List<? extends IModel.IComicModel> dataModel) {
    }

    @Override
    public void refreshData() {
    }

    @Override
    public void offLine() {
        isRemote = !isRemote;
    }
}
